package com.zp.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.OperationDictIcd10;
import com.zp.base.entity.bo.OperationDictIcd10Bo;
import com.zp.base.entity.vo.OperationDictIcd10Vo;
import com.zp.base.mapper.OperationDictIcd10Mapper;
import com.zp.base.service.IOperationDictIcd10Service;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 手术基础字典多个类型Service业务层处理
 *
 * @author zhang peng
 * @date 2023-01-11
 */
@RequiredArgsConstructor
@Service
public class OperationDictIcd10ServiceImpl implements IOperationDictIcd10Service {

    private final OperationDictIcd10Mapper baseMapper;

    /**
     * 查询手术基础字典多个类型
     */
    @Override
    public OperationDictIcd10Vo queryById (String operationDictId) {
        return baseMapper.selectVoById(operationDictId);
    }

    /**
     * 查询手术基础字典多个类型列表(分页)
     */
    @Override
    public TableDataInfo<OperationDictIcd10Vo> queryPageList (OperationDictIcd10Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OperationDictIcd10> lqw = buildQueryWrapper(bo);
        Page<OperationDictIcd10Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询手术基础字典多个类型列表(不分页)
     */
    @Override
    public List<OperationDictIcd10Vo> queryList (OperationDictIcd10Bo bo) {
        LambdaQueryWrapper<OperationDictIcd10> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OperationDictIcd10> buildQueryWrapper (OperationDictIcd10Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OperationDictIcd10> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getOperationCode()), OperationDictIcd10::getOperationCode, bo.getOperationCode());
        lqw.like(StringUtils.isNotBlank(bo.getOperationName()), OperationDictIcd10::getOperationName, bo.getOperationName());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationScale()), OperationDictIcd10::getOperationScale, bo.getOperationScale());
        lqw.eq(StringUtils.isNotBlank(bo.getStdIndicator()), OperationDictIcd10::getStdIndicator, bo.getStdIndicator());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovedIndicator()), OperationDictIcd10::getApprovedIndicator, bo.getApprovedIndicator());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagnosisOperType()), OperationDictIcd10::getDiagnosisOperType, bo.getDiagnosisOperType());
        lqw.eq(StringUtils.isNotBlank(bo.getInputCode()), OperationDictIcd10::getInputCode, bo.getInputCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), OperationDictIcd10::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增手术基础字典多个类型
     */
    @Override
    public Boolean insertByBo (OperationDictIcd10Bo bo) {
        OperationDictIcd10 add = BeanUtil.toBean(bo, OperationDictIcd10.class);
        validEntityBeforeSave(add);
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOperationDictId(add.getOperationDictId());
        }
        return flag;
    }

    /**
     * 修改手术基础字典多个类型
     */
    @Override
    public Boolean updateByBo (OperationDictIcd10Bo bo) {
        OperationDictIcd10 update = BeanUtil.toBean(bo, OperationDictIcd10.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (OperationDictIcd10 entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除手术基础字典多个类型
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
